/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

// needed for CoInitializeEx
#define _WIN32_DCOM
#include <Wbemidl.h>
#include <wincred.h>

#include "MWmiInfo.h"

enum MWMIWorkerErrStatus
{
	__wwesUnknown,
	__wwesOK,
	__wwesGetCredentials,
	__wwesAccessDenied,
	__wwesMemAllocError,
	__wwesNotConnected,
	__wwesNothingToQuery,
	__wwesClassDoesNotExists,
	__wwesOther
};

class MWMIWorker
{
private:
	IWbemLocator *locator;
	IWbemServices *services;

	TCHAR userName[CREDUI_MAX_USERNAME_LENGTH + 1];
	TCHAR userPass[CREDUI_MAX_PASSWORD_LENGTH + 1];
	SEC_WINNT_AUTH_IDENTITY authIdentity;

	bool SetStatus(HRESULT);

	bool interactiveRun;

	bool coIsInitialized;

public:
	MWMIWorker(void);
	~MWMIWorker(void);

	bool GetUserCredentials(void);
	bool GetProcessCredentials(void);
	void CleanUpUserCredentials(void);
	void SetUserCredentials(TCHAR*, TCHAR*, bool);

	bool InitComAndWmi(void);
	void CleanUpCom(void);

	bool ConnectToServer(CString, CString);		// IP address, namespace
	bool ConnectToServer(CString);				// network resource
	void DisconnectFromServer(void);

	bool ExecuteQuery(CString);

	bool ExecuteQueryInfo(MWmiInfo*);

	MWMIWorkerErrStatus status;
};

// ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/wmisdk/wmi/win32_classes.htm

// ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/wmisdk/wmi/wmi_providers.htm

// ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.WIN32COM.v10.en/wmisdk/wmi/creating_a_wmi_application_using_c_.htm
// ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/wmisdk/wmi/example__getting_wmi_data_from_a_remote_computer.htm